package com.gzyj.jtt808.v2011.protocol.subiao;



import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2011.additionalinfo.JTT808_AddtionalInfo;
import io.netty.buffer.ByteBuf;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Data
@ApiModel("激烈驾驶报警")
public class JTT808_0x0200_0x70 extends JTT808_AddtionalInfo implements Serializable {

    @ApiModelProperty("报警ID")
    public Long AlarmId;

    @ApiModelProperty("标志状态")
    public Byte FlagState;

    @ApiModelProperty("报警/事件类型")
    public Byte AlarmOrEventType;

    @ApiModelProperty("报警/事件类型 解析后")
    public String AlarmOrEventTypeString;

    @ApiModelProperty("报警时间阈值")
    public Integer AlarmTimeThreshold;

    @ApiModelProperty("报警阈值1")
    public Integer AlarmThreshold1;
    @ApiModelProperty("报警阈值2")
    public Integer AlarmThreshold2;

    @ApiModelProperty("车速")
    public Byte Speed;
    @ApiModelProperty("高程")
    public Integer Altitude;

    @ApiModelProperty("纬度")
    public Long Latitude;

    @ApiModelProperty("经度")
    public Long Longitude;

    @ApiModelProperty("日期时间")
    public Date AlarmTime;

    @ApiModelProperty("车辆状态")
    public Integer VehicleState;

    @ApiModelProperty("报警标识号")
    public AlarmIdentificationProperty AlarmIdentification;


    public JTT808_0x0200_0x70(){
        this.setID(0x70);
        this.setLength(47);
    }

    @Override
    public void encode(ByteBuf buf) {
        buf.writeByte(this.getID());
        buf.writeByte(this.getLength());

        buf.writeInt(this.AlarmId.intValue());
        buf.writeByte(this.FlagState);
        buf.writeByte(this.AlarmOrEventType);
        buf.writeShort(this.AlarmTimeThreshold);
        buf.writeShort(this.AlarmThreshold1);
        buf.writeShort(this.AlarmThreshold2);

        buf.writeByte(this.Speed);
        buf.writeShort(this.Altitude);
        buf.writeInt(this.Latitude.intValue());
        buf.writeInt(this.Longitude.intValue());
        buf.writeBytes(HexBytesUtil.WriteDateTime_yyMMddHHmmss(this.AlarmTime));
        buf.writeShort(this.VehicleState);
        this.AlarmIdentification.encode(buf);


    }

    @Override
    public JTT808_0x0200_0x70 decode(ByteBuf buf) {
        this.setLength(buf.readByte());

        this.AlarmId = buf.readUnsignedInt();
        this.FlagState = buf.readByte();

        this.AlarmOrEventType = buf.readByte();
        switch (this.AlarmOrEventType) {
            case 0x01:
                this.AlarmOrEventTypeString = "急加速报警";
                break;
            case 0x02:
                this.AlarmOrEventTypeString = "急减速报警";
                break;
            case 0x03:
                this.AlarmOrEventTypeString = "急转弯报警";
                break;

            case 0x04:
                this.AlarmOrEventTypeString = "怠速报警";
                break;
            case 0x05:
                this.AlarmOrEventTypeString = "异常熄火报警";
                break;
            case 0x06:
                this.AlarmOrEventTypeString = "空挡滑行报警";
                break;
            case 0x07:
                this.AlarmOrEventTypeString = "发动机超转报警";
                break;
            case 0x12:
            case 0x13:
            case 0x14:
            case 0x15:
            case 0x16:
                break;
        }

        this.AlarmTimeThreshold=buf.readUnsignedShort();
        this.AlarmThreshold1=buf.readUnsignedShort();
        this.AlarmThreshold2=buf.readUnsignedShort();

        this.Speed = buf.readByte();
        this.Altitude = buf.readUnsignedShort();
        this.Latitude = buf.readUnsignedInt();
        this.Longitude = buf.readUnsignedInt();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            this.AlarmTime = sdf.parse(HexBytesUtil.ReadDateTime_yyMMddHHmmss(HexBytesUtil.readByOffset(buf, 6)));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        this.VehicleState = buf.readUnsignedShort();

        this.AlarmIdentification = new AlarmIdentificationProperty();
        this.AlarmIdentification.decode(buf);

        return this;
    }
}
